<!-- 字典数据 -->
<script setup lang="ts">
import {
    getDictPage,
    getDictFormData,
    addDict,
    updateDict,
    deleteDict,
} from "@/api/dict";
import { DictPageVO, DictForm, DictQuery } from "@/api/dict/types";
import { Search } from '@element-plus/icons-vue'
defineOptions({
    name: "DictData",
    inheritAttrs: false,
});
const input3 = ref('')
const input4 = ref('')
const input5 = ref('')
const centerDialogVisible3 = ref(false)
const props = defineProps({
    typeCode: {
        type: String,
        default: () => {
            return "";
        },
    },
    typeName: {
        type: String,
        default: () => {
            return "";
        },
    },
});

watch(
    () => props.typeCode,
    (newVal: string) => {
        queryParams.typeCode = newVal;
        formData.typeCode = newVal;
        resetQuery();
    }
);

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<DictQuery>({
    pageNum: 1,
    pageSize: 10,
    typeCode: props.typeCode,
});

//const dictList = ref<DictPageVO[]>();

/* 选择框切换 */
const op = ref('指定费率')
const rateop = [
    {
        value: '指定费率',
        label: '指定费率',
    },
    {
        value: '单笔费额',
        label: '单笔费额',
    },
    {
        value: '组合收费',
        label: '组合收费',
    },
]



const dialog = reactive({
    title: "",
    visible: false,
});

const formData = reactive<DictForm>({
    status: 1,
    typeCode: props.typeCode,
    sort: 1,
});

const rules = reactive({
    name: [{ required: true, message: "请输入字典名称", trigger: "blur" }],
    value: [{ required: true, message: "请输入字典值", trigger: "blur" }],
});

/**
 * 查询
 */
function handleQuery() {
    if (queryParams.typeCode) {
        loading.value = true;
        getDictPage(queryParams)
            .then(({ data }) => {
                dictList.value = data.list;
                total.value = data.total;
            })
            .finally(() => (loading.value = false));
    }
}

/**
 * 重置查询
 */
function resetQuery() {
    queryFormRef.value.resetFields();
    queryParams.pageNum = 1;
    handleQuery();
}

/**
 * 行checkbox change事件
 *
 * @param selection
 */
function handleSelectionChange(selection: any) {
    ids.value = selection.map((item: any) => item.id);
}

/**
 * 打开字典表单弹窗
 *
 * @param dictId 字典ID
 */
function openDialog(dictId?: number) {
    dialog.visible = true;
    if (dictId) {
        dialog.title = "修改字典";
        getDictFormData(dictId).then(({ data }) => {
            Object.assign(formData, data);
        });
    } else {
        dialog.title = "新增字典";
    }
}

/**
 * 字典表单提交
 */
function handleSubmit() {
    dataFormRef.value.validate((isValid: boolean) => {
        if (isValid) {
            loading.value = false;
            const dictId = formData.id;
            if (dictId) {
                updateDict(dictId, formData)
                    .then(() => {
                        ElMessage.success("修改成功");
                        closeDialog();
                        resetQuery();
                    })
                    .finally(() => (loading.value = false));
            } else {
                addDict(formData)
                    .then(() => {
                        ElMessage.success("新增成功");
                        closeDialog();
                        resetQuery();
                    })
                    .finally(() => (loading.value = false));
            }
        }
    });
}

/**
 * 关闭弹窗
 */
function closeDialog() {
    dialog.visible = false;
    resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
    dataFormRef.value.resetFields();
    dataFormRef.value.clearValidate();

    formData.id = undefined;
    formData.status = 1;
    formData.sort = 1;
    formData.typeCode = props.typeCode;
}

/**
 * 删除字典
 */
function handleDelete(dictId?: number) {
    const dictIds = [dictId || ids.value].join(",");
    if (!dictIds) {
        ElMessage.warning("请勾选删除项");
        return;
    }

    ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
    }).then(() => {
        deleteDict(dictIds).then(() => {
            ElMessage.success("删除成功");
            resetQuery();
        });
    });
}

onMounted(() => {
    handleQuery();
});


/* 静态表格字段 */
const dictList = ref([
    {
        name: '商户1',
        value: 'M100001',
    },
    {
        name: '商户2',
        value: 'M100002',
    },
    {
        name: '商户3',
        value: 'M100003',
    },


]);
</script>

<template>
    <div class="app-container">
        <div class="" style="width: 90%;margin: 0 auto;  display:flex;justify-content:space-around;margin-top: -20px;">
            <p style="display:flex;width:290px;align-items:center;">
                <span
                    style="width:100px;height: 33px; display:flex;justify-content:flex-end;border:1px solid lightgray;padding:5px 0px;padding-right:10px;background-color:#8080800D">产品名称:</span>
                <span style="height: 33px;width: 250px; border: 1px solid lightgray;"></span>
            </p>

            <p style="display:flex;width:290px;align-items:center;">
                <span
                    style="width:100px;height:33px; display:flex;justify-content:flex-end;border:1px solid lightgray;padding:5px 0px;padding-right:10px;background-color:#8080800D">产品编码:</span>
                <span style="height: 33px;width: 250px; border: 1px solid lightgray;"></span>
            </p>
        </div>
        <el-form>
            <el-form-item label="默认费率" prop="" style="display: flex; align-items: center;">
                <el-select v-model="op" class="m-2" placeholder="" size="default" style="width: 40px;border: none;margin-left: -10px;">
                    <el-option v-for="item in rateop" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
                <!-- 当选择的值是 '指定费率' 时显示第一个输入框 -->
                <el-input v-if="op === '指定费率'" v-model="input1" style="width: 150px; margin-right: 15px;" type="number">
                    <template #append>
                        <el-button>%</el-button>
                    </template>
                </el-input>
                <el-input v-if="op === '组合收费'" v-model="input1" style="width: 150px; margin-right: 15px;" type="number">
                    <template #append>
                        <el-button>%</el-button>
                    </template>
                </el-input>
                <el-input v-if="op === '组合收费'" v-model="input2" style="width: 150px;">
                    <template #append>
                        <el-button>￥</el-button>
                    </template>
                </el-input>
                <!-- 当选择的值是 '组合收费' 时显示第二个输入框 -->
                <el-input v-else-if="op === '单笔费额'" v-model="input2" style="width: 150px;">
                    <template #append>
                        <el-button>￥</el-button>
                    </template>
                </el-input>
                <el-button type="primary" style="position: absolute;right: 0px;">一键填入</el-button>
            </el-form-item>

            <p><span>单独配置费率的商户：0</span> <el-input placeholder="输入商户ID/商户名称查找" style="margin-top: 10px;">
                    <template #append>
                        <el-button><el-icon>
                                <Search />
                            </el-icon></el-button>
                    </template>
                </el-input></p>
            <div style="border-top: 1px dotted lightgray;border-bottom: 1px dotted lightgray;">
                <el-empty :image-size="100" />
            </div>

        </el-form>
    </div>
</template>


<style lang="scss" scoped>
.input-with-select .el-input-group__prepend {
    background-color: var(--el-fill-color-blank);
}

.el-table__body {
    border: none;
    /* 移除表格主体的边框 */
}

.el-table__header-wrapper {
    border-bottom: none;
    /* 移除表头的底部边框 */
}

.m-2 .el-select {
    border: none !important;
}

/* 去除边框 */
:deep(.el-select .el-input__wrapper) {
    box-shadow: none !important;
}

/* 去除选中时蓝色边框（下面两个都要加上） */
:deep(.el-input .el-input__wrapper.is-focus) {
    //box-shadow: none !important;
}

:deep(.el-select .el-input.is-focus .el-input__wrapper) {
    border-color: #DCDFE6 !important;
    box-shadow: none !important;
}
</style>
